package com.ruyuan.seckilling.publish.async.listener;

import com.ruyuan.seckilling.common.ChannelKey;
import com.ruyuan.seckilling.publish.async.event.RemoveStaticPageEvent;
import com.ruyuan.starter.async.AsyncContext;
import com.ruyuan.starter.async.anno.Channel;
import com.ruyuan.starter.async.event.BaseEvent;
import com.ruyuan.starter.async.listener.EventListener;
import com.ruyuan.starter.async.thread.ExecutorService;
import com.ruyuan.starter.jedis.CacheSupport;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author zhonghuashishan
 */
@Slf4j
@Component
@Channel(ChannelKey.CHANNEL_REMOVE_STATIC_PAGE)
public class RemoveStaticPageEventListener implements EventListener<RemoveStaticPageEvent> {

    @Autowired
    private ExecutorService executorService;

    @Autowired
    private CacheSupport cacheSupport;

    @Override
    public boolean accept(BaseEvent event) {
        return event instanceof RemoveStaticPageEvent;
    }

    @Override
    public void onEvent(RemoveStaticPageEvent event, AsyncContext eventContext) {
        executorService.execute(ChannelKey.CHANNEL_REMOVE_STATIC_PAGE, () -> {
            cacheSupport.del(event.getStaticPageId());
            log.info("第4步，删除Redis上的静态页面");
        });
    }
}
